/**
 * Project Name:template-web
 * File Name   :ValidateCodeControl.java
 * Package Name:org.cn.template.component.web.control
 * Date:2015年9月27日下午4:49:11
 * Copyright (c) 2015, http://my.oschina.net/httpssl All Rights Reserved.
 *
*/

package org.cn.template.component.web.control;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * ClassName:ValidateCodeControl <br/>
 * Function :TODO ADD FUNCTION. <br/>
 * Reason	:TODO ADD REASON. <br/>
 * Date     :2015年9月27日 下午4:49:11 <br/>
 * @author  :http://my.oschina.net/httpssl
 * @email   :491835898@QQ.COM
 * @since   :JDK 1.7
 * @see 	
 * 验证码 
 */
@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@RequestMapping("validate_code")
public class ValidateCodeControl
{
	@RequestMapping(value="g.jspx",method = RequestMethod.GET)
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		// 设置页面不缓存
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		// 设置图片的长宽
		int width = 80, height = 20;
		// 设置备选汉字，剔除一些不雅的汉字
		String base = "ABbCcDdEeFfJHhKkLMmNnQRSTWXY34578";
		// 备选汉字的长度
		int length = base.length();
		// 创建内存图像
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		// 获取图形上下文
		Graphics g = image.getGraphics();
		// 创建随机类的实例
		Random random = new Random();
		// 设定图像背景色(因为是做背景，所以偏淡)
		// g.setColor(getRandColor(random, 200, 250));
		g.fillRect(0, 0, width, height);
		// 备选字体
		String[] fontTypes = { "Basemic Symbol", "Franklin Gothic Medium",
				"\u9ed1\u4f53", "\u6977\u4f53", "\u96b6\u4e66",
				"\u6977\u4f53_GB2312" };
		int fontTypesLength = fontTypes.length;
		// 在图片背景上增加噪点
		// g.setColor(getRandColor(random, 160, 200));
		g.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		// for (int i = 0; i < 6; i++) {
		// g.drawString("*/*/*/*/*/*/*/*/*/*/*/*", 0, 5 * (i + 2));
		// }
		// 取随机产生的认证码(6个汉字)
		// 保存生成的汉字字符串
		String sRand = "";
		for (int i = 0; i < 4; i++) {
			int start = random.nextInt(length);
			String rand = base.substring(start, start + 1);
			sRand += rand;
			// 设置字体的颜色
			g.setColor(getRandColor(random, 10, 150));
			// 设置字体
			g.setFont(new Font(fontTypes[random.nextInt(fontTypesLength)],
					Font.BOLD, 18 + random.nextInt(6)));
			// 将此汉字画到图片上
			g.drawString(rand, 18 * i + 8 + random.nextInt(8), 18);
			// 往汉字上画干扰线
			Line2D[] line = new Line2D.Double[1];
			Graphics2D g2 = (Graphics2D) g;
			for (int k = 0; k < line.length; k++) {
				line[k] = new Line2D.Double();
				double x1 = Math.random() * width;
				double x2 = Math.random() * width;
				double y1 = Math.random() * height;
				double y2 = Math.random() * height;
				line[k].setLine(y1, y2, x1, x2);
				g2.draw(line[k]);
			}
		}
		// 将认证码存入session
		request.getSession().setAttribute("rand", sRand);
		g.dispose();
		
		 ServletOutputStream sos = response.getOutputStream(); 
		// 输出图象到页面
		ImageIO.write(image, "JPEG", sos);
		sos.close(); 
	}

	// 随机产生颜色
	private Color getRandColor(Random random, int fc, int bc) {
		if (fc > 255)
			fc = 255;
		if (bc > 255)
			bc = 255;
		int r = fc + random.nextInt(bc - fc);
		int g = fc + random.nextInt(bc - fc);
		int b = fc + random.nextInt(bc - fc);
		return new Color(r, g, b);
	}
}
